﻿using Direct.IDAL.User.Jurisdiction;
using DirectSell.Common.Filter;
using DirectSell.Common.Page;
using DirectSell.Common.Session;
using DirectSell.Common.Url;
using DirectSell.IBLL;
using DirectSell.Model.User;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.WebLayer.Areas.User.Controllers
{
    /// <summary>
    /// 会员升级
    /// </summary>
    public class UserUpgradeController : Controller
    {
        #region 依赖注入
        [Dependency("UserUpGrade")]
        protected IOperation _upgrade { get; set; }

        [Dependency]
        protected IModifyInfoRespository _modify { get; set; }

        [Dependency]
        protected IUserUpgradeRepository _updal { get; set; }
        #endregion

        #region 首页
        /// <summary>
        /// 会员升级首页
        /// </summary>
        /// <returns></returns>
        [LoginFilter(IsCheck =true)]
        public async Task<ActionResult> Index(int page=1)
        {
            //会员信息
            var userinfo = await _modify.GetInfo(SessionInfo.GetSession().UserCode);
            //可升级列表
            var MemberLevels = _updal.QueryMemberLevel((byte)userinfo.UserLevel);
           
            //等级下拉框
            var selectItemList = new List<SelectListItem>() {
                new SelectListItem(){Value="0",Text="请选择会员级别",Selected=true}
            };

            var selectList = new SelectList(MemberLevels, "LevelID", "LevelName", "LevelID");

            selectItemList.AddRange(selectList);

            ViewBag.database = selectItemList;
            //会员升级记录
            var upgradeRecord = await _updal.Upgraderecord(userinfo.UserCode,page,CommonConfig.PageSize);

           // var pagedList = upgradeRecord.PageList(page);

            return View(upgradeRecord);
        }
        #endregion

        #region 操作
        /// <summary>
        /// 会员升级操作
        /// </summary>
        /// <returns></returns>
        public async Task<UserRespCode> Upgrade(UserUpGradeReq model)
        {
            var sessionmodel = SessionInfo.GetSession();
            model.UserCode = sessionmodel.UserCode;
            model.NowLevel = sessionmodel.UserLevel;
            model.AfterLevel = Convert.ToInt16(Request.Form["database-dropdownlist"]);
            if (model.AfterLevel == 0)
            {
                return UserRespCode.请选择会员级别;
            }
            if (!await _modify.CheckSecondPassword(model.UserCode, model.SecondPassword))
            {
                return UserRespCode.二级密码输入错误;
            }
            return await _upgrade.DoOperationAsync(model);
        }
        #endregion

        #region 计算差额
        /// <summary>
        /// 计算差额
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<string> Difference(UserUpGradeReq model)
        {
            model.NowLevel = SessionInfo.GetSession().UserLevel;
            var amount = string.Format("{00:f2}",await _updal.CalcAmount(model));
            return amount;
        }
        #endregion
    }
}